package com.carboncntr.helper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.carboncntr.tables.Customer;
import com.carboncntr.tables.History;
import com.carboncntr.util.HibernateUtil;

public class HistoryHelper {
	public static Long saveHistory(BigDecimal carbonAmt,Customer customer,Date entryDate,Date lastMod){
		Session session=HibernateUtil.getSessionfactory().openSession();
		Transaction transaction=null;
		Long historyId=null;
		try {
			transaction=session.beginTransaction();
			History history=new History();
			
			history.setCarbonAmt(carbonAmt);
			history.setCustomer(customer);
			history.setEntryDate(entryDate);
			history.setLastMod(lastMod);
			session.save(history);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		
		return historyId;
	}
	
	public static double getCarbonBalance(Long custId) {
		Session session = HibernateUtil.getSessionfactory().openSession();
		BigDecimal sum = new BigDecimal(0);
		try { 
			String SQL_QUERY = "select sum(h.carbonAmt) from History h join h.customer c where c.customerId=" + custId;
			
			Query query= session.createQuery(SQL_QUERY);
			List list=query.list();
			if(list != null && list.size() > 0) {
				sum=(BigDecimal)list.get(0);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally { 
			session.close(); 
		} 
		return sum != null ? sum.doubleValue(): 0;
	}

	public static List listHistory(Long customerId) 
	{ 
		Session session = HibernateUtil.getSessionfactory().openSession();
		List history=null;
		try { 
			Customer customer= (Customer) session.get(Customer.class, customerId); 
			history=customer.getHistory();
		} catch (HibernateException e) { 
		
			e.printStackTrace(); 
		} finally { 
			session.close(); 
		} 
		return history;
	}
	
	public static void updateHistory(Long historyId,BigDecimal carbonAmt,Customer customer,Date entryDate,Date lastMod) { 
		Session session = HibernateUtil.getSessionfactory().openSession();         
		Transaction transaction = null;         
		try {             
			transaction = session.beginTransaction();         
			History history= (History) session.get(History.class, historyId); 
			history.setCarbonAmt(carbonAmt); 
			transaction.commit();     
		} catch (HibernateException e) {             
			transaction.rollback();        
			e.printStackTrace(); 
		} finally { 
			session.close();     
		} 
	}
	public static void deleteHistory(int historyId){
		Session session = HibernateUtil.getSessionfactory().openSession(); 
		Transaction transaction = null; 
	        
		try {             
			transaction = session.beginTransaction();    
			/*History history= (History) session.get(History.class, historyId); 
			session.delete(history); */
			session.createQuery("delete from History where hist_id=" + historyId).executeUpdate();
			transaction.commit();     
		} catch (HibernateException e) {             
			transaction.rollback();        
			e.printStackTrace();     
		} finally {             
			session.close(); 	    
		}     
	} 


}
